/*
gobanjs - html5 SGF viewer for the Game of Go 

Copyright (C) 2013  Daniel Cioata
Visit http://www.govortex.ro/ for documentation, updates and examples.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/    

"use strict";

function Model(controller,index)
{
    this.index=index;
    this.controller=controller;
    this.parser=null;
    this.gametree=new GameTree();
    this.board=new Goban();
}

Model.prototype.init=function(rawsgf)
{
    this.parseSGF(rawsgf);    
    console.log("pwhite",this.getGameinfoProp(SGFProp.ID_PW));
    console.log("pblack",this.getGameinfoProp(SGFProp.ID_PB));   
    console.log("boardsize",this.getBoardSize());
    
    if(this.controller.config.autocrop==true)
    {
        this.adjustBounds(3);  
    }
    else
    {
        this.adjustBounds(19);
    }
    
    console.log("bounds",this.gametree.bounds); 
    this.board.init(this.getBoardSize());        
    this.goRoot();
    this.addProperties();
}

Model.prototype.getBounds=function()
{
    return this.gametree.bounds;
}

Model.prototype.isHandi=function(x,y)
{
    var hx,hy;
    hx = (x==3)||(x==9)||(x==15) ? true:false;
    hy = (y==3)||(y==9)||(y==15) ? true:false;
    if( hx && hy )
        return true;
    return false;
}

Model.prototype.adjustBounds=function(sz)
{
    var bd,bs;
    bd=this.gametree.bounds;
    bs=this.getBoardSize();
    bd.left -= sz;
    bd.right += sz;
    bd.top  -= sz;
    bd.bottom += sz;
    bd.left   = (bd.left < 0) ? 0 : bd.left ;
    bd.right  = (bd.right > bs ) ? bs : bd.right;
    bd.top    = (bd.top < 0) ? 0 : bd.top ;
    bd.bottom = (bd.bottom > bs ) ? bs : bd.bottom;
}

Model.prototype.parseSGF=function(rawsgf)
{
    if(this.parser==null)
    {
        this.parser=new Parser(this.gametree);
    }
    this.parser.parseSGF(rawsgf);
    /* console.log(rawsgf); */
}

Model.prototype.getGameinfoProp=function(p_id)
{
    var plist,i;
    plist=this.gametree.root.info.prop;
    if(plist!=null)
    {
        for(i=0;i<plist.length;i++)
        {
            if(p_id==plist[i].id)
                return plist[i].label;
        }
    }
    return null;    
}

Model.prototype.handleCommand=function(cmd,varnr)
{    
    var nd;
        
    if(cmd=="next")
    {
        this.moveNext(1);
    }
    else if(cmd=="end")
    {
        this.moveNext(-1);
    }
    else if(cmd=="prev")
    {
        this.movePrev(1);
    }
    else if(cmd=="start")
    {
        this.movePrev(-1);
    } 
    else if(cmd=="sgo_var")
    {
        if(varnr==0)
            this.moveNext(1);
        else
            this.moveChild(varnr-1);
    }
}

Model.prototype.addProperties=function()
{
    var i,node;
    node=this.getCurrentInfo();
    
    if(node==null)
        return;    
                    
    for(i=0;i<node.AW.length;i++)
    {        
        this.board.addMove(node.AW[i]);
    }
    for(i=0;i<node.AB.length;i++)
    {        
        this.board.addMove(node.AB[i]);
    }
}

Model.prototype.goRoot=function()
{
    this.gametree.goRoot();
}

Model.prototype.moveNext=function(step)
{    
    var i, node, status=false;
    
    if(step==-1)
        step=500;       
    for(i=0;i<step;i++)
    {
        status=this.gametree.moveNext();       
        if (status==false)
            break;
        node=this.getCurrentInfo();
        this.board.makeMove(node);        
    }
    return status;
}

Model.prototype.movePrev=function(steps)
{
    var i, node, status=false;
    
    if(steps==-1)
        steps=500;

    for(i=0;i<steps;i++)
    {
         node=this.gametree.getCurrentInfo();
         this.board.takeMove(node);
         status=this.gametree.movePrev();
         if(status==false)
            break;
    }
    return status;
}

Model.prototype.moveChild=function(ind)
{
    var node, status=false;    
    status=this.gametree.moveChild(ind);       
    if (status==false)
        return false;
    console.log(ind);        
    node=this.getCurrentInfo();
    this.board.makeMove(node);    
    return status;
}

Model.prototype.getBoardSize=function()
{        
    return this.gametree.gameinfo.boardsize;   
}

Model.prototype.getCurrent=function()
{
    return this.gametree.getCurrent();
}

Model.prototype.getCurrentInfo=function(steps)
{
    return this.gametree.getCurrentInfo()
}

Model.prototype.getNextNode=function()
{
    var node;   
    node=this.gametree.getCurrent();
    return node.next;
}

Model.prototype.getNextToPlay=function()
{
    var ntp,node,stone;
    ntp=this.getGameinfoProp(SGFProp.ID_PL);    
    if(ntp==null)
    {
        node=this.gametree.root.next;
        stone=node.getStone();
        if(stone!=null)
            ntp=stone.color;   
    }
    if(ntp!=null)
    {
        if(ntp==SGFProp.ID_B)
            return "Black to play";
        else if(ntp==SGFProp.ID_W)
            return "White to play";            
    }
    return "";
}

Model.prototype.getGameInfo=function()
{
    var gi;
    gi={"PW":"","PB":"","EV":""};
    gi.PW=this.getGameinfoProp(SGFProp.ID_PW);
    gi.PB=this.getGameinfoProp(SGFProp.ID_PB);
    gi.EV=this.getGameinfoProp(SGFProp.ID_EV);
    return gi;
}

